package com.accloud.utils;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import org.java_websocket.drafts.Draft_75;

public class LocalUtils {
    public static final String HOST_BROADCAST = "255.255.255.255";

    public static byte[] iterateBytes(ByteBuffer byteBuffer, int length) {
        byte[] bytes = new byte[length];
        for (int i = 0; i < length; i++) {
            bytes[i] = byteBuffer.get();
        }
        return bytes;
    }

    public static long parseLong(byte[] bytes) {
        long result = 0;
        for (byte b : bytes) {
            result = (result << 8) | ((long) (b & Draft_75.END_OF_FRAME));
        }
        return result;
    }

    public static DatagramPacket formatBroadcast(byte[] bytes, int port) throws UnknownHostException {
        return new DatagramPacket(bytes, bytes.length, InetAddress.getByName(HOST_BROADCAST), port);
    }
}
